package com.example.computer_exam_training_sys.controller;

import com.example.computer_exam_training_sys.entity.DiscussionTopicInfo;
import com.example.computer_exam_training_sys.service.DiscussionTopicService;
import com.example.computer_exam_training_sys.service.KnowledgePointService;
import com.example.computer_exam_training_sys.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/topic")
@CrossOrigin
public class DiscussionTopicController {

    @Autowired
    private DiscussionTopicService topicService;
    @Autowired
    private KnowledgePointService KnowledgePointService;

    @Autowired
    private QuestionService QuestionService;

    // 新增主题：POST http://localhost:8080/topic/add
    @PostMapping("/add")
    public int addTopic(@RequestBody DiscussionTopicInfo topic) {
        // 如果前端传了 courseId，自动查找 questionId
        if (topic.getCourseId() != null) {
            // 1. 通过 courseId 查找 knowledge_points 的 point_id
            Integer pointId = KnowledgePointService.getFirstPointIdByCourseId(topic.getCourseId());
            if (pointId != null) {
                // 2. 通过 pointId 查找 questions 的 question_id
                Integer questionId = QuestionService.getFirstQuestionIdByPointId(pointId);
                if (questionId != null) {
                    topic.setQuestionId(questionId);
                }
            }
        }
        return topicService.addTopic(topic);
    }
    // 删除主题：DELETE http://localhost:8080/topic/delete
    @DeleteMapping("/delete")
    public int deleteTopic(@RequestParam Integer topicId) {
        return topicService.deleteTopic(topicId);
    }

    // 获取主题列表：GET http://localhost:8080/topic/list
    @GetMapping("/list")
    public Map<String, Object> getTopicList(
            @RequestParam(required = false) Integer topicId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {

        List<DiscussionTopicInfo> list = topicService.getTopicListByPage(topicId, page, size);
        int total = topicService.countTopics(topicId);

        Map<String, Object> result = new HashMap<>();
        result.put("list", list);
        result.put("total", total);
        return result;
    }

    // 获取单个主题：GET http://localhost:8080/topic/get?id=1
    @GetMapping("/get")
    public DiscussionTopicInfo getTopicById(@RequestParam Integer id) {
        return topicService.getTopicById(id);
    }

    @GetMapping("/with-comments")
    public List<DiscussionTopicInfo> getTopicWithComments(@RequestParam(required = false) Integer topicId) {
        return topicService.getTopicWithComments(topicId);
    }

    @GetMapping("/with-comments-details")
    public List<Map<String, Object>> getTopicWithCommentsDetails(@RequestParam Integer topicId) {
        return topicService.getTopicWithCommentsDetails(topicId);
    }

    // 搜索主题及其评论：GET http://localhost:8080/topic/search
    @GetMapping("/search")
    public Map<String, Object> searchTopics(
            @RequestParam(required = false) Integer topicId,
            @RequestParam(required = false) String title,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {

        List<DiscussionTopicInfo> list = topicService.searchTopicsByTopicIdOrTitle(topicId, title, page, size);
        int total = topicService.countSearchTopics(topicId, title);

        Map<String, Object> result = new HashMap<>();
        result.put("list", list);
        result.put("total", total);
        return result;
    }

    // 获取主题列表（带科目名）：GET http://localhost:8080/topic/list-with-course
    @GetMapping("/list-with-course")
    public Map<String, Object> getTopicListWithCourseName(
            @RequestParam(required = false) Integer topicId,
            @RequestParam(required = false) String courseName,
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size
    ) {
        List<DiscussionTopicInfo> list = topicService.getTopicListWithCourseName(topicId, courseName, keyword, page, size);
        int total = topicService.countTopicsWithCourseName(topicId, courseName, keyword);
        Map<String, Object> result = new HashMap<>();
        result.put("list", list);
        result.put("total", total);
        return result;
    }

    @GetMapping("/top-courses")
    public List<Map<String, Object>> getTopCourses() {
        return topicService.getTopCourses();
    }

    @GetMapping("/hot-topics")
    public List<Map<String, Object>> getHotTopics() {
        return topicService.getHotTopics();
    }

    // DiscussionTopicController.java
    @PostMapping("/increase-view-count")
    public int increaseViewCount(@RequestParam Integer topicId) {
        return topicService.increaseViewCount(topicId);
    }
}